package {

	import flash.display.Sprite;
	import org.molamil.acto.states.AbstractIo3Manager;
	import flash.events.Event;	
	
	/**
	 * @author Jorge Hernandez
	 */
	public class TransitionManager extends AbstractIo3Manager {

		
		// PROPERTIES
		
		private var alphaTo:Number;
		private var alphaStep:Number = 0.05;
		
		
		// CONSTRUCTOR
		
		public function TransitionManager(target:Object = null) {
			super(target);
		}

		
		// PUBLIC METHODS
		
		public override function destroy():void {
			spriteTarget.removeEventListener(Event.ENTER_FRAME, targetInEfHandler);
			spriteTarget.removeEventListener(Event.ENTER_FRAME, targetOutEfHandler);
			target = null;
		}
		
		
		// PROTECTED METHODS
		
		protected override function doIn():void {
			spriteTarget.alpha = 0;
			alphaTo = 1;
			spriteTarget.addEventListener(Event.ENTER_FRAME, targetInEfHandler);
		}

		
		protected override function doOut():void {
			alphaTo = 0;
			spriteTarget.addEventListener(Event.ENTER_FRAME, targetOutEfHandler);
		}
		
		
		// EVENT HANDLERS
		
		private function targetInEfHandler(event:Event):void {
			spriteTarget.alpha += alphaStep;
			if (spriteTarget.alpha >= alphaTo) {
				spriteTarget.alpha = 1;
				spriteTarget.removeEventListener(Event.ENTER_FRAME, targetInEfHandler);
				state = STATE_ON;
			}
		}
		
		private function targetOutEfHandler(event:Event):void {
			spriteTarget.alpha -= alphaStep;
			if (spriteTarget.alpha <= alphaTo) {
				spriteTarget.alpha = 0;
				spriteTarget.removeEventListener(Event.ENTER_FRAME, targetOutEfHandler);
				state = STATE_OFF;
			}
		}
		
		
		// ACCESSORS
		
		public function get spriteTarget():Sprite {
			return Sprite(target);
		}

		
	}
	
}
